<?php
// 本类由系统自动生成，仅供测试用途
namespace Home\Controller;

use Think\Controller;
use Think\Model;

class IndexController extends Controller {
	//定义静态状态变量表示状态
	
	public function Plsession() {
		if (! session ( 'user_id' ) || session ( 'header' ) != $_SERVER ['REMOTE_ADDR'] . $_SERVER ['HTTP_USER_AGENT']) {
			
			$this->redirect ( 'Login/Index' );
			exit ();
		} else {
			session_regenerate_id ();
			
		}
	}
	public function title() {
		$suId = session ( 'user_id' );
		if (! $suId) {
			function get_url() {
			$sys_protocal = isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == '443' ? 'https://' : 'http://';
			$php_self = $_SERVER['PHP_SELF'] ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME'];
			$path_info = isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '';
			$relate_url = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : $php_self.(isset($_SERVER['QUERY_STRING']) ? '?'.$_SERVER['QUERY_STRING'] : $path_info);
			return $sys_protocal.(isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '').$relate_url;
		 }
 
      $url=get_url();
	        $this->assign('url',"$url");
			$this->assign ( 'session', '' );
		} else {
			$this->assign ( 'session', md5 ( $suId ) );
			$uCont = M ( 'user' )->where ( "user_id=$suId" )->field ( array (
				'user_id',
				'user_name',
				'user_img',
				'user_linvite',
				'user_text' 
		) )->find ();
		$this->assign ( 'uCont', $uCont );
		$messCont = M ( 'mess' )->where ( "accepe_user_id=$suId and mess_pass=0" )->select ();
		foreach ( $messCont as $m ) {
			$userContMess = M ( 'user' )->where ( "user_id=$m[send_user_id]" )->field ( array (
					'user_id',
					'user_name',
					'user_img' 
			) )->find ();
			$articleMess = M ( 'article' )->where ( "article_id=$m[article_id]" )->field ( 'article_id', 'article_title', 'article_content' )->find ();
			$arrMess [] = array (
					'uMid' => $m ['send_user_id'],
					'uMname' => $userContMess ['user_name'],
					'uMimg' => $userContMess ['user_img'],
					'aMid' => $m ['article_id'],
					'aMtitle' => $articleMess ['article_title'],
					'aMcontent' => substr ( strip_tags(htmlspecialchars_decode ( $articleMess ['article_content'])), 0, 150 ) ,
					'Mid' => $m ['mess_id'],
					'MessContent' => $m ['mess_content'],
					'MessType' => $m ['mess_type'],
					'MessTime' => date ( "Y-m-d H:i:s", $m ['mess_time'] ) 
			);
		}
		$messNum = M ( 'mess' )->where ( "accepe_user_id=$suId and mess_pass=0" )->Count ();
		$messNum = $messNum != 0 ? $messNum : '';
		$this->assign ( 'messNum', $messNum );
		$this->assign ( 'messList', $arrMess );
		}
		
		
	}
	
	public function MessPush() {
		$this->Plsession();
		set_time_limit(0);
		if (IS_AJAX) {
			$suId = session ( 'user_id' );
			$data ['suId'] = $suId;
			while ( true ) {
				$messNum = M ( 'mess' )->where ( "accepe_user_id= $suId and mess_pass=0" )->Count ();
				if (session ( 'messNum' ) == null) {
					session ( 'messNum',$messNum );
				}
				if ($messNum == session ( 'messNum' ) ) {
					sleep ( 10 );
					continue;
				} else {
					session ( 'messNum',$messNum );
					$uCont = M ( 'user' )->where ( "user_id=$suId" )->field ( array (
							'user_id',
							'user_name',
							'user_img',
							'user_linvite',
							'user_text'
					) )->find ();
					$data ['uCont'] = $uCont;
					$messCont = M ( 'mess' )->where ( "accepe_user_id=$suId and mess_pass=0" )->select ();
					foreach ( $messCont as $m ) {
						$userContMess = M ( 'user' )->where ( "user_id=$m[send_user_id]" )->field ( array (
								'user_id',
								'user_name',
								'user_img'
						) )->find ();
						$articleMess = M ( 'article' )->where ( "article_id=$m[article_id]" )->field ( 'article_id', 'article_title', 'article_content' )->find ();
						$arrMess [] = array (
								'uMid' => $m ['send_user_id'],
								'uMname' => $userContMess ['user_name'],
								'uMimg' => $userContMess ['user_img'],
								'aMid' => $m ['article_id'],
								'aMtitle' => $articleMess ['article_title'],
								'aMcontent' => htmlspecialchars_decode ( substr ( $articleMess ['article_content'], 1, 150 ) ),
								'Mid' => $m ['mess_id'],
								'MessContent' => $m ['mess_content'],
								'MessType' => $m ['mess_type'],
								'MessTime' => date ( "Y-m-d H:i:s", $m ['mess_time'] )
						);
					}
					$data ['messNum'] = $messNum;
					$data ['messList'] = $arrMess;
	
					$this->ajaxReturn($data);
				}
			}
		} else
			$this->error ( "非法访问" );
	}
	
	public function MessSeek() { // 这是消息找你的方法
		$this->Plsession ();
		if (IS_AJAX) {
			$userId = session ( 'user_id' );
			$userHe = I ( 'umid', '', 'int' );
			$amId = I ( 'amid', '', 'int' );
			$mid = I ( 'mid', '', 'int' );
			$mtype = I ( 'mtype', '', 'int' );
			switch ($mtype) {
				case 3 :
					$type = 2;
					$messType = 6;
					$articleClass = 'article_discuss';
					$userIn = $userHe;
					$userSuccess = 'user_discuss';
					break; // 这是文章
				case 4 :
					$type = 1;
					$messType = 7;
					$articleClass = 'article_marking';
					$userIn = $userHe;
					$userSuccess = 'user_marking';
					break; // 这是文章申请批阅
				case 5 :
					$type = 2;
					$messType = 8;
					$articleClass = 'article_discuss';
					$userIn = $userId;
					$userSuccess = 'user_discuss';
					break;
				default :
					exit ();
			}
			$artMess = M ( 'article' )->where ( "article_id=$amId and article_type=$type" )->getField ( $articleClass );
			$arrDiscuss = explode ( '@', $artMess );
			if (in_array ( $userIn, $arrDiscuss )) {
				$messPass = M ( 'mess' )->where ( "mess_id=$mid and send_user_id=$userHe and accepe_user_id=$userId" )->setField ( 'mess_pass', '1' );
				$messCont = M ( 'mess' )->where ( "accepe_user_id=$userId and mess_pass=0" )->Count ();
				echo "$messCont";
				exit ();
			}
			$artMess = $artMess ? $artMess . '@' . $userIn : $userIn;
			$aetSet = M ( 'article' )->where ( "article_id=$amId and article_type=$type and article_user_id=$userId" )->setField ( $articleClass, $artMess );
			if (! $userHe)
				exit ();
			$success = M ( 'user' )->where ( "user_id=$userIn" )->getField ( $userSuccess );
			$success = $success ? $success . '@' . $amId : $amId;
			M ( 'user' )->where ( "user_id=$userIn" )->setField ( $userSuccess, $success );
			$messPass = M ( 'mess' )->where ( "mess_id=$mid and send_user_id=$userHe and accepe_user_id=$userId" )->setField ( 'mess_pass', '1' );
			$dataMess = array (
					'article_id' => $amId,
					'mess_type' => $messType,
					'send_user_id' => $userId,
					'accepe_user_id' => $userHe,
					'mess_time' => time () 
			);
			M ( 'mess' )->data ( $dataMess )->add ();
			$messCont = M ( 'mess' )->where ( "accepe_user_id=$userId and mess_pass=0" )->Count ();
			echo "$messCont";
			$this->state_mess();
		} else {
			$this->error ( '非法访问' );
		}
	}
	
	public function MessIgnore() { // 这是消息忽略
		$this->Plsession ();
		$userId = session ( 'user_id' );
		$mid = I ( 'mid', '', 'int' );
		$mtype = I ( 'mtype', '', 'int' );
		$messPass = M ( 'mess' )->where ( "mess_id=$mid and accepe_user_id=$userId" )->setField ( 'mess_pass', '2' );
		$messCont = M ( 'mess' )->where ( "accepe_user_id=$userId and mess_pass=0" )->Count ();
		$messCont = $messCont != 0 ? $messCont : '';
		echo "$messCont";
	}
	
	public function messRead() {
		$this->Plsession ();
		$userId = session ( 'user_id' );
		$type = I ( 'type', '', 'int' );
		if ($type == 1) {
			$where ['mess_type'] = array (
					'in',
					'1,2' 
			);
			$where ['accepe_user_id'] = array (
					'eq',
					$userId 
			);
			$messPass = M ( 'mess' )->where ( $where )->setField ( 'mess_pass', '1' );
		} elseif ($type == 2) {
			$where ['mess_type'] = array (
					'in',
					'6,7,8' 
			);
			$where ['accepe_user_id'] = array (
					'eq',
					$userId 
			);
			$messPass = M ( 'mess' )->where ( $where )->setField ( 'mess_pass', '1' );
		}
		$messCont = M ( 'mess' )->where ( "accepe_user_id=$userId and mess_pass=0" )->Count ();
		$messCont = $messCont != 0 ? $messCont : '';
		echo "$messCont";
	}
	
	public function messClose() {
		$this->Plsession ();
		$userId = session ( 'user_id' );
		$messId = session ( 'mess_id' );
		$type = I ( 'type', '', 'int' );
		if ($type == 1) {
	 		$where ['mess_type'] = array (
							'in',
							'1,2'
					);
			$where ['accepe_user_id'] = array (
							'eq',
							$userId
					);
			$messPass = M ( 'mess' )->where ( $where )->setField ( 'mess_pass', '1' );
		} elseif ($type == 2) {
				$where ['mess_type'] = array (
						'in',
						'6,7,8'
				);
			$where ['accepe_user_id'] = array (
						'eq',
						$userId
			);
			$messPass = M ( 'mess' )->where ( $where )->setField ( 'mess_pass', '1' );
		}
		$messCont = M ( 'mess' )->where ( "accepe_user_id=$userId and mess_pass=0" )->Count ();
		$messCont = $messCont != 0 ? $messCont : '';
		echo "$messCont";
	}

	public function index() {
 		if (! session ( 'user_id' )) {
 			$this->redirect ( 'Index/hall' );
 		}
		$this->Plsession ();
		$userId = session ( "user_id" );
		$max_id = I ( 'max_id', null, 'int' );
		
		if (empty ( $max_id )) {
			$data = $this->doIndex ( $userId );
			$this->assign ( 'list', $data ['list'] ); // 这是将文章和用户关系对应起来
			$this->assign ( 'messTop', $data ['messTop'] );
			$this->assign ( 'uimg', $data ['uimg'] );
			$max_id = $data ['list'] [count ( $data ['list'] ) - 1] ['article_id'];
			$this->assign ( 'max_id', $max_id ? $max_id : 0);
			$this->assign('recommend',$data ['recommend']);
			$this->display ( "index" );
		} else if (IS_POST) {
			$data = $this->doIndex ( $userId, $max_id );
			$this->ajaxReturn($data);
		} else
			$this->error ( "非法访问" );
	}
	
	private function doIndex($userId, $max_id = 0) {
		$guanzhu = M ( 'reltion' )->where ( "reltion=$userId" )->getField ( 'is_reltion', true );
		
		if (! in_array ( $userId, $guanzhu ))
			$guanzhu[] = $userId;
		
		$map = '(article_user_id in (';
		
		for($i = 0;$i < count($guanzhu);$i++)
		{
			if($guanzhu[$i] == $userId)
				continue;
			if($i > 1)
				$map .= ',';
			$map .= $guanzhu[$i];
		}
		$map .= ')';
		$map .= ' or ';
		$map .='(';
		$map .= 'article_user_id = '.$userId.' ';
		$map .= ' and ';
		$map .= 'article_type in (1,2) ))';
		if ($max_id != 0) {
			$map .= ' and ';
			$map .= 'article_id < '.$max_id;
		}
		// $map['article_father_id']=array('eq','0');
		// $count = M ( 'article' )->where ( $map )->count ();
		$article = M ( 'article' )->where ( $map )->order ( 'article_id desc' )->limit ( 0, 10 )->select ();
		$userCont = M ( 'user' )->order ( 'user_id desc' )->select ();
		$arr = array ();
		$arrCont = array ();
		foreach ( $article as $a ) {
			if ($a ['article_father_id'] != 0) {
				if ($a ['article_user_id'] == $userId)
					continue;
				$art = M ( 'article' )->where ( "article_id=$a[article_father_id]" )->find ();
				$article_zan = $art ['article_zan'];
				$article_zan_id = $art ['article_zan_id'];
				$article_dao = $art ['article_dao'];
				$article_dao_id = $art ['article_dao_id'];
				$a['article_num'] = $art['article_num'];
				$arrZan = explode ( "@", $article_zan_id );
				$article_zan_id = in_array ( $userId, $arrZan ) ? 1 : 0;
				$arrDao = explode ( "@", $article_dao_id );
				$article_dao_id = in_array ( $userId, $arrDao ) ? 1 : 0;
				$arrOrder = explode ( "@", $art ['article_order'] );
				$is_order = in_array ( $userId, $arrOrder ) ? 1 : 0;
				$order = $art ['article_order'] ? count ( $arrOrder ) : 0;
			} else {
				
				$article_zan = $a ['article_zan'];
				$article_zan_id = $a ['article_zan_id'];
				$article_dao = $a ['article_dao'];
				$article_dao_id = $a ['article_dao_id'];
				$arrZan = explode ( "@", $article_zan_id );
				$article_zan_id = in_array ( $userId, $arrZan ) ? 1 : 0;
				$arrDao = explode ( "@", $article_dao_id );
				$article_dao_id = in_array ( $userId, $arrDao ) ? 1 : 0;
				$arrOrder = explode ( "@", $a ['article_order'] );
				$is_order = in_array ( $userId, $arrOrder ) ? 1 : 0;
				$order = $a ['article_order'] ? count ( $arrOrder ) : 0;
			}
			foreach ( $userCont as $u ) {
				if ($a ['article_user_id'] == $u ['user_id']) {
					$arrCont [] = array (
							'article_id' => $a ['article_id'],
							'article_father_id' => $a ['article_father_id'],
							'article_title' => $a ['article_title'],
							'article_num' => $a ['article_num'],
							'article_content' => htmlspecialchars_decode ( $a ['article_content'] ),
							'article_type' => $a ['article_type'],
							'article_time' => date ( "Y-m-d H:i:s", $a ['article_time'] ),
							'father_id' => $a ['article_father_id'],
							'article_order' => $order,
							'is_order' => $is_order,
							'article_zan' => $article_zan,
							'article_zan_id' => $article_zan_id,
							'article_dao' => $article_dao,
							'article_dao_id' => $article_dao_id,
							'user_id' => $u ['user_id'],
							'user_email' => $u ['user_email'],
							'user_img' => $u ['user_img'],
							'user_name' => $u ['user_name'] 
					);
				}
			}
		}
		if (IS_POST) {
			$arrCont = $arrCont ? $arrCont : array();
			return $arrCont;
		} else {
			$result ['list'] = $arrCont; // 这是将文章和用户关系对应起来
			
			//得到推荐的好友
			$recommend = $this->recommend($userId,6);
			
			$recommendUserId = array();
			foreach ($recommend as $r){
				$recommendUserId[] = $r["user_id"];
			}
		
			$reltion = M ( 'reltion' )->where ( "reltion=$userId and is_reltion!=$userId" )->getField ( 'is_reltion', true );
			$data ['user_id'] = array (
					array(
						'in',
						$reltion 
					),
					array(
						'not in',
						$recommendUserId
					)
			);
			$userReltion = M ( 'user' )->field ( array (
					'user_id',
					'user_name',
					'user_img' 
			) )->where ( $data )->select ();
			
			
			$this->title ();
			$messTop = D ( 'mess' )->where ( "accepe_user_id=$userId and mess_pass=0 and (mess_type=9 or mess_type=10)" )->relation ( true )->find ();
			if ($messTop) {
				M ( 'mess' )->where ( "mess_id=$messTop[mess_id]" )->setField ( 'mess_pass', 1 );
			}
			$result ['messTop'] = $messTop;
			$result ['uimg'] = $userReltion;
			$result['recommend'] = $recommend;
			return $result;
		}
	}
	public function addArticle() { // 这是发表文章的方法
		$this->Plsession ();
		if (IS_AJAX) {
			$user_id = session ( 'user_id' );
			if (! $user_id)
				exit ();
			$invite = I ( 'invite', '', 'htmlspecialchars' );
			$article_father_id = I ( 'articleId', '', 'int' );
			if ($article_father_id) {
				$art = M ( 'article' )->where ( "article_id=$article_father_id" )->find ();
				$article_zan = $art ['article_zan'];
				$article_zan_id = $art ['article_zan_id'];
				$article_dao = $art ['article_dao'];
				$article_dao_id = $art ['article_dao_id'];
				$article_order = $art ['article_order'];
				$orderNum = explode ( "@", $article_order );
				$is_order = in_array ( $user_id, $orderNum ) ? 1 : 0;
				$order = count ( $order );
				$arrZan = explode ( "@", $article_zan_id );
				$article_zan_id = in_array ( $user_id, $arrZan ) ? 1 : 0;
				$arrDao = explode ( "@", $article_dao_id );
				$article_dao_id = in_array ( $user_id, $arrDao ) ? 1 : 0;
			} else {
				$order = "0";
				$is_order = "0";
			}
			
			if ($article_zan_id == 0 && $article_dao_id == 0) {
				$discuss = I ( 'user_discuss', '', 'htmlspecialchars' );
				$data = array (
						'article_title' => I ( 'title', '', 'htmlspecialchars' ), // 默认已经过滤！
						'article_content' => I ( 'content', '', 'htmlspecialchars' ),
						'article_time' => time (),
						'article_type' => I ( 'articleType', '', 'int' ),
						'article_user_id' => session ( 'user_id' ),
						'article_father_id' => $article_father_id,
						'article_discuss' => $user_id . $invite 
				);
				$addArticle = M ( 'article' )->field ( array (
						'article_type',
						'article_title',
						'article_content',
						'article_time',
						'article_user_id',
						'article_father_id',
						'article_discuss' 
				) )->data ( $data )->add ();
				if ($addArticle) {
					if ($article_father_id) {
						M ( 'article' )->where ( "article_id=$article_father_id" )->setInc ( 'article_zan' );
						$artcile_zan_id = M ( 'article' )->where ( "article_id=$article_father_id" )->getField ( 'article_zan_id' );
						$artcile_zan_id = $artcile_zan_id ? $artcile_zan_id . '@' . $user_id : $user_id;
						$updataArticle = M ( 'article' )->where ( "article_id=$article_father_id" )->setField ( 'article_zan_id', "$artcile_zan_id" );
						$Sarticle = M ( 'article' )->where ( "article_id=$article_father_id" )->find ();
					} else {
						$Sarticle = M ( 'article' )->where ( "article_id=$addArticle" )->find ();
					}
					$user = M ( 'user' )->where ( "user_id=$user_id" )->find ();
					$arr = array (
							'article_id' => $Sarticle ['article_id'],
							'article_title' => $Sarticle ['article_title'],
							'article_content' => htmlspecialchars_decode ( $Sarticle ['article_content'] ),
							'article_type' => $Sarticle ['article_type'],
							'article_time' => date ( "Y-m-d H:i:s", $Sarticle ['article_time'] ),
							'article_order' => $order,
							'article_zan' => $Sarticle ['article_zan'],
							'article_zan_id' => $Sarticle ['article_zan_id'],
							'article_dao' => $Sarticle ['article_dao'],
							'is_order' => $is_order,
							'article_dao_id' => $Sarticle ['article_dao_id'],
							'user_email' => $user ['user_email'],
							'user_name' => $user ['user_name'],
							'user_img' => $user ['user_img'],
							'user_name' => $user ['user_name'] 
					);
					echo json_encode ( $arr );
					if ($invite) {
						$arrUId = explode ( "@", $invite );
						foreach ( $arrUId as $u ) {
							if ($u != $user_id && $u != '') {
								$dataMess = array (
										'article_id' => $addArticle,
										'mess_type' => 5,
										'send_user_id' => $user_id,
										'accepe_user_id' => $u,
										'mess_time' => time () 
								);
								M ( 'mess' )->data ( $dataMess )->add ();
							}
						}
					}
				} else {
					echo "false";
				}
			} else {
				echo "false";
			}
		}
		$this->state_mess();
	}
	public function daoAcrticle() { // 这是文章点到的方法
		$this->Plsession ();
		if (IS_AJAX) {
			$user_id = session ( 'user_id' );
			$article_id = I ( 'articleId', '', 'int' );
			$art = M ( 'article' )->where ( "article_id=$article_id" )->find ();
			$article_zan = $art ['article_zan'];
			$article_zan_id = $art ['article_zan_id'];
			$article_dao = $art ['article_dao'];
			$article_dao_id = $art ['article_dao_id'];
			$arrZan = explode ( "@", $article_zan_id );
			$article_zan_id = in_array ( $user_id, $arrZan ) ? 1 : 0;
			$arrDao = explode ( "@", $article_dao_id );
			$article_dao_id = in_array ( $user_id, $arrDao ) ? 1 : 0;
			if ($article_zan_id == 0 && $article_dao_id == 0) {
				M ( 'article' )->where ( "article_id=$article_id" )->setInc ( 'article_dao' );
				$artcile_dao_id = M ( 'article' )->where ( "article_id=$article_id" )->getField ( 'article_dao_id' );
				$artcile_dao_id = $artcile_dao_id ? $artcile_dao_id . '@' . $user_id : $user_id;
				$updataArticle = M ( 'article' )->where ( "article_id=$article_id" )->setField ( 'article_dao_id', "$artcile_dao_id" );
				$Sarticle = M ( 'article' )->where ( "article_id=$article_id" )->find ();
				$arr = array (
						article_dao => $Sarticle ['article_dao'],
						article_zan_id => $Sarticle ['article_zan_id'] 
				);
				echo json_encode ( $arr );
			}
		}
	 $this->state_mess();
	}
	public function Minfo($infoId) {
		$userInfo = M ( 'user' )->where ( "user_id=$infoId" )->field ( array (
				'user_id',
				'user_name',
				'user_img',
				'user_praise',
				'user_marking',
				'user_discuss',
				'user_text' 
		) )->find ();
		$article = M ( 'article' )->where ( "article_user_id=$infoId and article_father_id=0" )->select ();
		$articleInfo = count($article);
		
		//和    人
		$arrDiscuss = array();
		foreach ($article as $a)
		{
			if( $a['article_type'] == 2 )
			{
				$arrDiscuss = $arrDiscuss + explode ( "@", $a['article_discuss'] );
			}
		}
		array_flip(array_flip($arrDiscuss));
		$allDiscussNumberCount = count($arrDiscuss);
		// 条观点
		$temp = M( 'article_operate' )->where( "operate_user_id = $infoId and operate_type = 2" )->group('article_id')->getField("count(*)",true);
		$operateCount = array_sum($temp);
		$discuss = count($temp);
		
		
		$friendInfo = M ( 'reltion' )->where ( "reltion = $infoId" )->Count ();
		$Praise = M ( 'article' )->where ( "article_user_id=$infoId and article_father_id=0" )->getField ( 'article_id', true );
		$wherePraise ['article_father_id'] = array (
				'in',
				$Praise 
		);
		$wherePraise ['article_type'] = array (
				'eq',
				'4' 
		);
		$articlePraise = M ( 'article' )->where ( $wherePraise )->Count ();
		$arrInfo = array (
				'text' => $userInfo ['user_text'],
				'name' => $userInfo ['user_name'],
				'img' => $userInfo ['user_img'],
				'praise' => $userInfo ['user_praise'],
				'marking' => count ( explode ( "@", $userInfo ['user_marking'] ) ),
				'discuss' => $discuss,
				'articleNum' => $articleInfo,
				'friend' => $friendInfo,
				'art_zan' => $articlePraise,
				'disNumCount' => $allDiscussNumberCount,
				'operateCount' => $operateCount
		);
		$this->assign ( 'Minfo', $arrInfo );
	}
	public function mypage() { // 这是我的主页
		$this->Plsession ();
		$userId = session ( "user_id" );
		$max_id = I ( 'max_id', '0', 'int' );
		// $guanzhu=M('reltion')->where("reltion=$userId")->getField('is_reltion',true);
		// if(!in_array($userId,$guanzhu))$guanzhu[]=$userId;
		$map ['article_user_id'] = array (
				'eq',
				$userId 
		);
		$count = M ( 'article' )->where ( $map )->count ();
	
		if($max_id != 0)
			$map['article_id'] = array('lt',$max_id);
		
		$article = M ( 'article' )->where ( $map )->order ( 'article_id desc' )->limit ( 0, 10 )->select ();
		$userCont = M ( 'user' )->order ( 'user_id desc' )->select ();
		$arr = array ();
		$arrCont = array ();
		foreach ( $article as $a ) {
			if ($a ['article_father_id'] != 0) {
				$art = M ( 'article' )->where ( "article_id=$a[article_father_id]" )->find ();
				$article_zan = $art ['article_zan'];
				
				$article_zan_id = $art ['article_zan_id'];
				$article_dao = $art ['article_dao'];
				$article_dao_id = $art ['article_dao_id'];
				$a['article_num'] = $art['article_num'];
				$arrZan = explode ( "@", $article_zan_id );
				$article_zan_id = in_array ( $userId, $arrZan ) ? 1 : 0;
				$arrDao = explode ( "@", $article_dao_id );
				$article_dao_id = in_array ( $userId, $arrDao ) ? 1 : 0;
				$arrOrder = explode ( "@", $art ['article_order'] );
				$is_order = in_array ( $userId, $arrOrder ) ? 1 : 0;
				$order = $art ['article_order'] ? count ( $arrOrder ) : 0;
			} else {
				
				$article_zan = $a ['article_zan'];
				$article_zan_id = $a ['article_zan_id'];
				$article_dao = $a ['article_dao'];
				$article_dao_id = $a ['article_dao_id'];
				$arrZan = explode ( "@", $article_zan_id );
				$article_zan_id = in_array ( $userId, $arrZan ) ? 1 : 0;
				$arrDao = explode ( "@", $article_dao_id );
				$article_dao_id = in_array ( $userId, $arrDao ) ? 1 : 0;
				$arrOrder = explode ( "@", $a ['article_order'] );
				$is_order = in_array ( $userId, $arrOrder ) ? 1 : 0;
				$order = $a ['article_order'] ? count ( $arrOrder ) : 0;
			}
			foreach ( $userCont as $u ) {
				if ($a ['article_user_id'] == $u ['user_id']) {
					$arrCont [] = array (
							'article_id' => $a ['article_id'],
							'article_father_id' => $a ['article_father_id'],
							'article_title' => $a ['article_title'],
							'article_content' => htmlspecialchars_decode ( $a ['article_content'] ),
							'article_type' => $a ['article_type'],
							'article_time' => date("Y-m-d H:i",$a ['article_time']),
							'father_id' => $a ['article_father_id'],
							'article_order' => $order,
							'is_order' => $is_order,
							'article_zan' => $article_zan,
							'article_zan_id' => $article_zan_id,
							'article_dao' => $article_dao,
							'article_dao_id' => $article_dao_id,
							'user_text' => $u ['user_text'],
							'user_id' => $u ['user_id'],
							'user_email' => $u ['user_email'],
							'user_img' => $u ['user_img'],
							'user_name' => $u ['user_name'],
							'article_num' => $a ['article_num']
					);
				}
			}
		}
		if (IS_POST) {
			
			echo json_encode ( $arrCont );
		} else {
			$this->assign ( 'list', $arrCont ); // 这是将文章和用户关系对应起来
			$this->assign ( 'max_id', $arrCont[count($arrCont) - 1]['article_id']);
			$this->title ();
			$this->Minfo ( $userId );
			$this->myTag ( $userId );
			$this->display ();
		}
	}
	public function hispage() { // 这是他的主页
		$userId = session ( "user_id" );
		$reltionId = I ( 'hispage', '', 'int' );
		if ($userId == $reltionId)
			$this->assign ( 'hispass', '1' );
		if ($reltionId)
			session ( 'his_id', $reltionId );
		if (IS_POST) {
			$reltionId = session ( 'his_id' );
		}
		$max_id = I ( 'max_id', '0', 'int' );
		$count = M ( 'article' )->where ( "article_user_id=$reltionId" )->count ();
		
		if($max_id != 0)
			$map['article_id'] = array('lt',$max_id);
		
		$map['article_user_id'] = array('eq',$reltionId);
		
		$article = M ( 'article' )->where ( $map )->order ( 'article_id desc' )->limit ( 0, 10 )->select ();
		
		$userCont = M ( 'user' )->where ( "user_id=$reltionId" )->select ();
		$arr = array ();
		$arrCont = array ();
		foreach ( $article as $a ) {
			if ($a ['article_father_id'] != 0) {
				$art = M ( 'article' )->where ( "article_id=$a[article_father_id]" )->find ();
				$article_zan = $art ['article_zan'];
				$article_zan_id = $art ['article_zan_id'];
				$article_dao = $art ['article_dao'];
				$article_dao_id = $art ['article_dao_id'];
				$a['article_num'] = $art['article_num'];
				$arrOrder = explode ( "@", $art ['article_order'] );
				$order = $art ['article_order'] ? count ( $arrOrder ) : 0;
				if (! $userId) {
					$article_zan_id = 0;
					$article_dao_id = 0;
					$is_order = 0;
				} else {
					$arrZan = explode ( "@", $article_zan_id );
					$article_zan_id = in_array ( $userId, $arrZan ) ? 1 : 0;
					$arrDao = explode ( "@", $article_dao_id );
					$article_dao_id = in_array ( $userId, $arrDao ) ? 1 : 0;
					$is_order = in_array ( $userId, $arrOrder ) ? 1 : 0;
				}
			} else {
				$article_zan = $a ['article_zan'];
				$article_zan_id = $a ['article_zan_id'];
				$article_dao = $a ['article_dao'];
				$article_dao_id = $a ['article_dao_id'];
				$arrOrder = explode ( "@", $a ['article_order'] );
			    $order = $a ['article_order'] ? count ( $arrOrder ) : 0;
				if (! $userId) {
						$article_zan_id = 0;
						$article_dao_id = 0;
						$is_order = 0;
				} else {
					$arrZan = explode ( "@", $article_zan_id );
					$article_zan_id = in_array ( $userId, $arrZan ) ? 1 : 0;
					$arrDao = explode ( "@", $article_dao_id );
					$article_dao_id = in_array ( $userId, $arrDao ) ? 1 : 0;
					$is_order = in_array ( $userId, $arrOrder ) ? 1 : 0;
					
					//echo $is_order.' '.$art ['article_order'];
				}
				
			}
			foreach ( $userCont as $u ) {
				if ($a ['article_user_id'] == $u ['user_id']) {
					$arrCont [] = array (
							'article_id' => $a ['article_id'],
							'article_father_id' => $a ['article_father_id'],
							'article_title' => $a ['article_title'],
							'article_content' => htmlspecialchars_decode ( $a ['article_content'] ),
							'article_type' => $a ['article_type'],
							'article_time' => date("Y-m-d H:i",$a ['article_time']),
							'father_id' => $a ['article_father_id'],
							'article_order' => $order,
							'is_order' => $is_order,
							'article_zan' => $article_zan,
							'article_zan_id' => $article_zan_id,
							'article_dao' => $article_dao,
							'article_dao_id' => $article_dao_id,
							'user_id' => $u ['user_id'],
							'user_email' => $u ['user_email'],
							'user_img' => $u ['user_img'],
							'user_name' => $u ['user_name'],
							'article_num' => $a ['article_num']
					);
				}
			}
		}
		if (IS_POST) {
			echo json_encode ( $arrCont );
		} else {
			$this->assign ( 'list', $arrCont ); // 这是将文章和用户关系对应起来
			$this->assign ('max_id', $arrCont[count($arrCont) - 1]['article_id']);
			//dump( $arrCont[count($arrCont) - 1]['article_id']);
			$reltion = M ( 'reltion' )->where ( "reltion=$userId" )->getField ( 'is_reltion', true );
			in_array ( $reltionId, $reltion ) ? $this->assign ( 'is', 1 ) : $this->assign ( 'is', 0 );
			$heart = M ( 'user' )->where ( "user_id=$reltionId" )->getField ( 'user_praise_id' );
			$this->assign ( 'data', $userCont );
			$this->title ();
			$this->Minfo ( $reltionId );
			$this->myTag ( $reltionId );
			$this->display ();
		}
	}
	public function attention() { // 这是他的页面点击关注的方法
		$this->Plsession ();
		if (IS_AJAX) {
			$userId = session ( 'user_id' );
			$fileId = I ( 'userId', '', 'int' );
			$reltion = M ( 'reltion' )->field ( array (
					'is_reltion' 
			) )->where ( "reltion=$userId" )->select ();
			foreach ( $reltion as $r ) {
				$arr [] = $r ['is_reltion'];
			}
			if (! in_array ( $fileId, $arr )) {
				$this->assign ( 'list', $arrCont );
				$data = array (
						'reltion' => $userId,
						'is_reltion' => $fileId 
				);
				$file = M ( 'reltion' )->field ( array (
						'reltion',
						'is_reltion' 
				) )->data ( $data )->add ();
				if ($file) {
					// echo $fileId;
					echo "true";
					
					$reltion = M ( 'reltion' )->field ( array (
							'is_reltion' 
					) )->where ( "reltion=$fileId" )->select ();
					
					foreach ( $reltion as $r ) {
						$arr [] = $r ['is_reltion'];
					}
					if (in_array ( $userId, $arr )) {
						
						$dataMess = array (
								'mess_type' => 9,
								'send_user_id' => $userId,
								'accepe_user_id' => $fileId,
								'mess_time' => time () 
						);
						M ( 'mess' )->field ( array (
								'mess_type',
								'send_user_id',
								'accepe_user_id',
								'mess_time' 
						) )->data ( $dataMess )->add ();
						$dataMess1 = array (
								'mess_type' => 9,
								'send_user_id' => $fileId,
								'accepe_user_id' => $userId,
								'mess_time' => time () 
						);
						M ( 'mess' )->data ( $dataMess1 )->add ();
					}
				} else {
					echo "false";
				}
			}
		}
	$this->state_mess();
	}
	public function heart() { // 这是点击桃心的方法
		$this->Plsession ();
		if (IS_AJAX) {
			$userId = session ( 'user_id' );
			$fileId = I ( 'userId', '', 'int' );
			$user = M ( 'user' )->where ( "user_id=$fileId" )->getField ( 'user_praise_id' );
			$arr = explode ( "@", $user );
			if (! in_array ( $userId, $arr )) {
				M ( 'user' )->where ( "user_id=$fileId" )->setInc ( 'user_praise' );
				$user = $user ? $user . "@" . $userId : $userId;
				M ( 'user' )->where ( "user_id=$fileId" )->setField ( 'user_praise_id', $user );
				$userCont = M ( 'user' )->where ( "user_id=$fileId" )->find ();
                 echo $userCont['user_praise'];
			} else {
				$userCont = M ( 'user' )->where ( "user_id=$fileId" )->find ();
				echo $userCont ['user_praise'];
			}
		}
	}

	public function discuss () { // 这是讨论页面方法
		if (IS_GET) {
			$userId = session ( 'user_id' );
			$articleId = I ( 'aId', '', 'int' );
			$article = M ( 'article' )->where ( "article_id=$articleId and article_type=2 and article_father_id=0" )->find ();
			M ( 'article' )->where ( "article_id=$articleId and article_type=2 and article_father_id=0" )->setField ( 'article_num', $article['article_num'] + 1);
			if ($article) {
				if($article['article_user_id'] == $userId)
					$writer = true;
				else 
					$writer = false;
				$userDis = M ( 'user' )->where ( "user_id=$article[article_user_id]" )->find ();
				$arrDiscuss = explode ( "@", $article ['article_discuss'] );
				foreach ( $arrDiscuss as $a ) {
						$user = M ( 'user' )->where ( "user_id=$a" )->find ();
						$arrInfo [] = array (
								'img' => $user ['user_img'],
								'name' => $user ['user_name'],
								'Uid' => $user ['user_id'] 
						);
					}
				if (! $userId) {
					
					$this->assign ( 'discuss', '0' );
					$this->assign ( 'order', '0' );
				} else {
					if (in_array ( $userId, $arrDiscuss ))
						$this->assign ( 'discuss', '1' );
					

					$arrOrder = explode ( "@", $article ['article_order'] );
					
					if (in_array ( $userId, $arrOrder ))
						$this->assign ( 'order', '1' );
				}
				$order = $article ['article_order'] ? count ( $arrOrder ) : 0;
				$arrDis = array (
						'articleId' => $article ['article_father_id'] == 0 ? $article ['article_id'] : $article ['article_father_id'],
						'articleUId' => $article ['article_user_id'],
						'title' => $article ['article_title'],
						'content' => htmlspecialchars_decode ( $article ['article_content'] ),
						'order' => $order,
						'discuss' => $article ['article_discuss'],
						'utime' => $article ['article_time'],
						'name' => $userDis ['user_name'],
						'img' => $userDis ['user_img'],
						'uId' => $userDis ['user_id'],
						'num' => $article['article_num'] + 1
				);
				$operate = M ( 'article_operate' )->where ( "article_id=$articleId and operate_type=2" )->select ();
				$arrOperate = array ();
				foreach ( $operate as $op ) {
					$userCont = M ( 'user' )->where ( "user_id=$op[operate_user_id]" )->field ( array (
							'user_id',
							'user_name',
							'user_img' 
					) )->find ();
					$arrOperate [] = array (
							'oPcontent' => $op ['operate_content'],
							'oPtime' => date ( 'Y-m-d H:i:s', $op ['operate_time'] ),
							'Ftime' => $op ['operate_time'],
							'uname' => $userCont ['user_name'],
							'uid' => $userCont ['user_id'],
							'uimg' => $userCont ['user_img'] 
					);
				}
				$this->assign ( 'operate', $arrOperate );
				$aTime = time () - $article ['article_time'];
				$aTime > 172800 ? $this->assign ( 'aTime', '1' ) : $this->assign ( 'aTime', '0' );
				$this->assign ( 'arrDis', $arrDis );
				$this->assign ( 'info', $arrInfo );
			} else {
				$this->error ( '访问失败' );
			}
		} else {
			$this->error ( '非法访问本页面' );
		}
           if( $userId ){
				$messis=M('mess')->where("send_user_id = $userId and accepe_user_id = $article[article_user_id] and article_id=$article[article_id]")->getField('mess_id');
				
			}

		//得到推荐的好友
		$recommend = $this->recommend($userId,6);
		
		$recommendUserId = array();
		foreach ($recommend as $r){
			$recommendUserId[] = $r["user_id"];
		}
		
		$reltion = M ( 'reltion' )->where ( "reltion=$userId" )->getField ( 'is_reltion', true );
		$data ['user_id'] = array (
				array(
						'in',
						$reltion
				),
				array(
						'not in',
						$recommendUserId
				)
		);
		$userReltion = M ( 'user' )->field ( array (
				'user_id',
				'user_name',
				'user_img'
		) )->where ( $data )->select ();
		
			
		$mis=$messis?1:0;
		$this->assign('mis',$mis);
		$this->assign('appImg',$article ['article_discuss']);
		$this->assign('uimg',$userReltion);
		$this->assign('writer',$writer);
		$this->assign('recommend',$recommend);
		$this->title ();
		$this->display ( "single-discuss" );
	}

	/**
 	* 此方法是获取推荐的好友
 	* @param integer $userId 
 	* @param integer $count 数量
 	*/
	public function recommendFriends($userId=9,$count=4){
		$Model = new Model();
		return $Model->query("SELECT
											a.user_id,
											user_name,
											user_img
										FROM
											think_user_tag AS a
										JOIN think_user AS b ON a.user_id = b.user_id
										WHERE
											tag_id IN (
												SELECT
													tag_id
												FROM
													think_user_tag
												WHERE
													user_id = $userId
											)
										AND tag_type = 2
										AND a.user_id IN (
											SELECT
												is_reltion
											FROM
												think_reltion
											WHERE
												reltion = $userId
										)
										AND a.user_id != $userId
										GROUP BY
											a.user_id
										ORDER BY
											COUNT(tag_id) DESC,
											a.user_id
										LIMIT 0,$count");
	}
	
	/**
	 * 此方法是获取推荐的人（全网）
	 * @param integer $userId
	 * @param integer $count 数量
	 */
	public function recommend($userId=9,$count=4){
		$Model = new Model();
		return $Model->query("SELECT
				a.user_id,
				user_name,
				user_img
				FROM
				think_user_tag AS a
				JOIN think_user AS b ON a.user_id = b.user_id
				WHERE
				tag_id IN (
				SELECT
				tag_id
				FROM
				think_user_tag
				WHERE
				user_id = $userId
		)
				AND tag_type = 2
				AND a.user_id != $userId
				GROUP BY
				a.user_id
				ORDER BY
				COUNT(tag_id) DESC,
				a.user_id
				LIMIT 0,$count");
	}
	
	public function discussInvite() { // 讨论页面邀请新的朋友
		$this->Plsession ();
		if (IS_AJAX) {
			$userId = session ( 'user_id' );
			$articleId = I ( 'articleId','','int' );
			$invite = substr(I ( 'invite','','' ), 1);
			$article = M ( 'article' )->where( "article_id=$articleId" )->find();
			if($article['article_user_id'] == $userId)//確認是發佈者
			{
				$new_invite_array = explode(  '@' , $invite );
				$exists_invite_array = explode( "@",$article['article_discuss'] );
				if(!$this->_array_repeat( $new_invite_array , $exists_invite_array ) && count($exists_invite_array) <= 12)//確認不能包含原來的參與者以及人数不能超过12
				{
					echo M ( 'article' )->where ( "article_id=$articleId" )->setField ( 'article_discuss', $article['article_discuss']."@".$invite );
					
					//给新成员发送通知
					if ($invite) {
						$arrUId = explode ( "@", $invite );
						foreach ( $arrUId as $u ) {
							if ($u != $userId && $u != '') {
								$dataMess = array (
										'article_id' => $articleId,
										'mess_type' => 5,
										'send_user_id' => $userId,
										'accepe_user_id' => $u,
										'mess_time' => time ()
								);
								M ( 'mess' )->data ( $dataMess )->add ();
							}
						}
					}
					$this->ajaxReturn();
				}
			}
		}
	}
	private function _array_repeat($b,$a){
		foreach ($a as $va) {
			if (in_array($va, $b)) {
				return true;
			}else {
				continue;
			}
		}
		return false;
	}
	
	public function discussReword() { // 讨论页面的回复
		$this->Plsession ();
		if (IS_AJAX) {
			$content = I ( 'content', '', '' );
			$articleId = I ( 'articleId', '', 'int' );
			$aTime = I ( 'aTime', '', 'int' );
			$userId = session ( 'user_id' );
			if (empty ( $content ))
				exit ();
			$time = time ();
			$tada = array (
					'article_id' => $articleId,
					'operate_user_id' => $userId,
					'operate_type' => '2',
					'operate_content' => $content,
					'operate_time' => $time 
			);
			$aTime = time () - $aTime;
			if ($aTime > 86400 * 2) {
				exit ();
			}
			$operate = M ( 'article_operate' )->field ( 'article_id', 'operate_user_id', 'optype', 'operate_content', 'operate_time' )->data ( $tada )->add ();
			if ($operate) {
				$discuss = M ( 'article' )->where ( "article_id=$articleId" )->getField ( 'article_discuss' );
				$arrDiscuss = explode ( "@", $discuss );
				$content = $_POST ['content'];
				$user = M ( 'user' )->where ( "user_id=$userId" )->find ();
				$arr = array (
						ucontent => $content,
						ftime => $time,
						utime => date ( 'Y-m-d H:i', $time ),
						uimg => $user ['user_img'],
						uname => $user ['user_name'],
						uid => $userId 
				);
				foreach ( $arrDiscuss as $accepe ) {
					if ($accepe == $userId)
						continue;
					$dataDiscuss = array (
							'article_id' => $articleId,
							'mess_type' => 1,
							'send_user_id' => $userId,
							'accepe_user_id' => $accepe,
							'mess_content' => $content,
							'mess_time' => time () 
					);
					M ( 'mess' )->data ( $dataDiscuss )->add ();
				}
				echo json_encode ( $arr );
			}
		} 
		$this->state_mess();//消息查询
	}
	public function requestJoin() { // 这是申请加入的方法
		$this->Plsession ();
		if (IS_AJAX) {
			$articleId = I ( 'article', '', 'int' );
			$accepe = M ( 'article' )->where ( "article_id=$articleId" )->getField ( 'article_user_id' );
			$userId = session ( 'user_id' );
			$data = array (
					'article_id' => $articleId,
					'mess_type' => 3,
					'send_user_id' => $userId,
					'accepe_user_id' => $accepe,
					'mess_time' => time () 
			);
			$mess = M ( 'mess' )->data ( $data )->add ();
			echo $mess;
		}
		$this->state_mess();
	}
	public function discussOrder() { // 这是订阅讨论的方法
		$this->Plsession ();
		if (IS_AJAX) {
			$userId = session ( 'user_id' );
			$fatherId = I ( 'articleId', '', 'int' );
			$title = I ( 'title', '', 'htmlspecialchars' );
			$content = I ( 'content', '', 'htmlspecialchars' );
			$article = M ( 'article' )->where ( "article_id = $fatherId" )->find();
			while($article ['article_father_id'] != 0)
			{
				$article = M ( 'article' )->where( "article_id = ".$article ['article_father_id'] )->find();
				$fatherId = $article ['article_id'];
			}
			if (in_array ( $userId, explode ( "@", $article['article_order'] ) ))
				exit ();
			$fatherId=$article['article_father_id']?$article['article_father_id']:$fatherId ;
			$arrArticle = array (
					'article_user_id' => $userId,
					'article_father_id' => $fatherId,
					'article_type' => 6,
					'article_title' => $title,
					'article_content' => $content,
					'article_time' => time () 
			);
			$articleAdd = M ( 'article' )->field ( 'article_user_id', 'article_father_id', 'article_type', 'article_title', 'article_content', 'article_time' )->data ( $arrArticle )->add ();
			$article = $article['article_order'] ? $article['article_order'] . '@' . $userId : $userId;
			M ( 'article' )->where ( "article_id=$fatherId" )->setField ( 'article_order', $article );
			echo $fatherId;
			$orderNum = count ( explode ( "@", $article ) ) ? count ( explode ( "@", $article ) ) : 0;
			$userCont = M ( 'user' )->where ( "user_id=$userId" )->field ( 'user_id', 'user_name', 'user_img' )->find ();
			$arr = array (
					'uid' => $userId,
					'user_name' => $userCont ['user_name'],
					'user_img' => $userCont ['user_img'],
					'article_content' => $content,
					'article_title' => $title,
					'is_order' => 1,
					'atime' => time (),
					'articleType' => 6,
					'article_order' => $orderNum 
			);
			echo json_encode ( $arr );
		}
	$this->state_mess();
	}
	/*public function discussLook() { // 这是讨论查看的方法
		$this->Plsession ();
		if (IS_AJAX) {
			set_time_limit ( 0 );
			$timeNes = I ( 'aTime', '', 'int' );
			$aId = I ( 'aId', '', 'int' );
			$time = time ();
			while ( true ) 

			{
				
				if ((time () - $timeNes) == 86400) {
					$order = M ( 'article' )->where ( "article_id=$aId" )->getField ( "article_order" );
					$arrOrder = explode ( "@", $order );
					foreach ( $arrOrder as $arrId ) {
						$data = array (
								'article_id' => $aId,
								'mess_type' => 10,
								'send_user_id' => 1,
								'accepe_user_id' => $arrId,
								'mess_time' => time () 
						);
						$mess = M ( 'mess' )->data ( $data )->add ();
					}
				}
				$userId = session ( 'user_id' );
				$operate = M ( 'article_operate' )->where ( "operate_time>$time and operate_user_id!=$userId and operate_type=2" )->find ();
				if (! empty ( $operate )) {
					$user = M ( 'user' )->where ( "user_id=$operate[operate_user_id]" )->find ();
					$arr = array (
							ucontent => htmlspecialchars_decode ( $operate ['operate_content'] ),
							ftime => time (),
							utime => date ( "Y-m-d H:i:s", $operate ['operate_time'] ),
							uimg => $user ['user_img'],
							uname => $user ['user_name'] 
					);
					echo json_encode ( $arr );
					break;
				}
			}
			sleep ( 1 );
		}
	}
	*/
	// 这是文章批阅的方法
	public function article() {
		if (IS_GET) {
			$userId = session ( 'user_id' );
			$articleId = I ( 'aid', '', 'int' );
			$article = M ( 'article' )->where ( "article_id=$articleId and article_father_id=0 and article_type=1" )->find ();
			M ( 'article' )->where ( "article_id=$articleId and article_father_id=0 and article_type=1" )->setField('article_num', $article['article_num'] + 1);
			if (! $article)
				$this->error ( "非法访问本页面" );
			$user = M ( 'user' )->field ( 'user_id', 'user_name', 'user_img' )->where ( "user_id=$article[article_user_id]" )->find ();
			$arrCont = array (
					'atitle' => $article ['article_title'],
					'acontent' => htmlspecialchars_decode ( $article ['article_content'] ),
					'atime' => $article ['article_time'],
					'aid' => $articleId,
					'uid' => $article ['article_user_id'],
					'uname' => $user ['user_name'],
					'uimg' => $user ['user_img'],
					'num' => $article['article_num']
			);
			
			$arrMark = explode ( "@", $article ['article_marking'] );
			if (! $userId)
				$is = 0;
			
			elseif( $userId == $article ['article_user_id'] ){
				$is=1;
			}else{
				$is = in_array ( $userId, $arrMark ) ? 1 : 0;
			}
			if( $userId ){
				$messis=M('mess')->where("send_user_id = $userId and accepe_user_id = $article[article_user_id] and article_id=$article[article_id]")->getField('mess_id');
				
				}
			$mis=$messis?1:0;
			$this->assign('mis',$mis);
			$this->assign ( 'is', $is );
			$this->assign ( 'list', $arrCont );
			$this->title ();
			$this->display ( 'single-article' );
			// 这是点击批阅文章的加载项，后期可以删除的模块
			/*
			 * $operate=D('article_operate')->where("article_id=$articleId and operate_type=1")->relation(true)->select(); $this->assign('markingCont',$operate);
			 */
		} else {
			$this->error ( "非法访问本页面" );
		}
	}
	public function Marking() { // 这是提交批阅内容的方法
		$this->Plsession ();
		if (IS_AJAX) {
			$userId = session ( 'user_id' );
			$content = I ( 'content', '', 'htmlspecialchars' );
			$articleId = I ( 'aId', '', 'int' );
			$accepe = M ( 'article' )->where ( "article_id=$articleId" )->getField ( 'article_user_id' );
			$data = array (
					'article_id' => $articleId,
					'operate_user_id' => $userId,
					'operate_type' => 1,
					'operate_content' => $content,
					'operate_time' => time () 
			);
			$oId = M ( 'article_operate' )->data ( $data )->add ();
			$dataMarking = array (
					'article_id' => $articleId,
					'mess_type' => 2,
					'send_user_id' => $userId,
					'accepe_user_id' => $accepe,
					'mess_content' => $content,
					'mess_time' => time () 
			);
			M ( 'mess' )->data ( $dataMarking )->add ();
			$arr = array (
					'aId' => $articleId 
			);
			echo $oId;
		}
		$this->state_mess();
	}
	public function applyMarking() { // 这是申请批阅的方法
		$this->Plsession ();
		if (IS_AJAX) {
			$userId = session ( 'user_id' );
			I ( 'auid', '', 'int' );
			I ( 'aid', '', 'int' );
			$dataMarking = array (
					'article_id' => I ( 'aid', '', 'int' ),
					'mess_type' => 4,
					'send_user_id' => $userId,
					'accepe_user_id' => I ( 'auid', '', 'int' ),
					'mess_time' => time () 
			);
			$apply = M ( 'mess' )->data ( $dataMarking )->add ();
			if ($apply) {
				echo 'true';
			}
		}
	$this->state_mess();
	}
	public function updataArticle() {
		$this->Plsession ();
		if (IS_AJAX) {
			$articleId = I ( 'aId', '', 'int' );
			$articleCont = I ( 'articleCont', '', 'htmlspecialchars' );
			$a = M ( 'article' )->where ( "article_id=$articleId" )->setField ( 'article_content', $articleCont );
			echo $a;
		}
	}
	public function lookMarking() {
		if (IS_AJAX) {
			$operateId = I ( 'valueId', '', 'int' );
			$operate = D ( 'article_operate' )->where ( "operate_id=$operateId and operate_type=1" )->relation ( true )->find ();
			$arrCont = array (
					'user_name' => $operate ['user_name'],
					'user_img' => $operate ['user_img'],
					'operate_content' => $operate ['operate_content'],
					'operate_time' => date ( "Y-m-d H:i:s", $operate ['operate_time'] ) 
			);
			echo json_encode ( $arrCont );
		}
	}
	public function message() { // 这是消息的方法
		$this->Plsession ();
		$userId = session ( 'user_id' );
		$messCont1 = M ( 'mess' )->where ( "accepe_user_id=$userId" )->order ( 'mess_id desc' )->select ();
		foreach ( $messCont1 as $m ) {
			$userContMess1 = M ( 'user' )->where ( "user_id=$m[send_user_id]" )->field ( 'user_id', 'user_name', 'user_img' )->find ();
			$articleMess1 = M ( 'article' )->where ( "article_id=$m[article_id]" )->field ( 'article_id', 'article_title', 'article_content' )->find ();
			$aMcontent = $articleMess1 ['article_title'] ? $articleMess1 ['article_title'] : $articleMess1 ['article_content'];
			$arrMess1 [] = array (
					'uMid' => $m ['send_user_id'],
					'uMname' => $userContMess1 ['user_name'],
					'uMimg' => $userContMess1 ['user_img'],
					'aMid' => $m ['article_id'],
					'aMtitle' => $articleMess1 ['article_title'],
					'aMcontent' => substr ( strip_tags(htmlspecialchars_decode ( $aMcontent )), 0, 151 ),
					'Mid' => $m ['mess_id'],
					'Mpass' => $m ['mess_pass'],
					'MessContent' => $m ['mess_content'],
					'MessType' => $m ['mess_type'],
					'MessTime' => date ( "Y-m-d H:i:s", $m ['mess_time'] ) 
			);
		}
		$dataSave ['mess_pass'] = '1';
		$messCont = M ( 'mess' )->where ( "accepe_user_id=$userId and mess_pass=0 and (mess_type!=3 or mess_type!=4 or mess_type!=5)" )->save ( $dataSave );
		$this->title ();
		$this->Minfo ( $userId );
		$this->assign ( 'messCont', $arrMess1 );
		$this->display ();
	}
	public function friend() { // 这是朋友页面
		$this->Plsession ();
		$userId = session ( 'user_id' );
		$reltion = M ( 'reltion' )->where ( "reltion=$userId" )->getField ( 'is_reltion', true );
		$is_reltion = M ( 'reltion' )->where ( "is_reltion=$userId" )->getField ( 'reltion', true );
		$arrUser = array_intersect_assoc ( $reltion, $is_reltion );
		foreach ( $arrUser as $au ) {
			$key = array_search ( $au, $reltion );
			array_splice ( $reltion, $key, 1 );
		}
		$map ['user_id'] = array (
				'in',
				$reltion 
		); // 这是关注的数组ID
		$map1 ['user_id'] = array (
				'in',
				$arrUser 
		); // 这是好友的数组ID
		$Attention = M ( 'user' )->where ( $map )->field ( 'user_time', 'user_id', 'user_name', 'user_img' )->select (); // 这是关注查询
		$this->assign ( 'Attention', $Attention );
		$friend = M ( 'user' )->where ( $map1 )->field ( 'user_time', 'user_id', 'user_name', 'user_img' )->select (); // 这是好友查询
		$this->assign ( 'friend', $friend );
		$this->Minfo ( $userId );
		$this->title ();
		$this->display ();
	}
	public function setup() { // 这是设置方法
		$this->Plsession ();
		$userId = session ( 'user_id' );
		$uCont = M ( 'user' )->where ( "user_id=$userId" )->find ();
		$this->assign ( 'uCont', $uCont );
		$this->myTag ( $userId );
		$this->title ();
		$this->display ();
	}
	public function upload() {
		$this->Plsession ();
		$upload = new \Think\Upload (); // 实例化上传类
		$upload->maxSize = 3145728; // 设置附件上传大小
		$upload->exts = array (
				'jpg',
				'gif',
				'png',
				'jpeg' 
		); // 设置附件上传类型
		$upload->rootPath = './Uploads/'; // 设置附件上传根目录
		$upload->savePath = '/images/'; // 设置附件上传（子）目录
		                                // 上传文件
		$upload->saveName = time () . '_' . mt_rand ();
		
		$info = $upload->upload ();
		
		if (! $info) { // 上传错误提示错误信息
			echo $upload->getError ();
		} else { // 上传成功 获取上传文件信息
			$this->ImageCut ( "Uploads/images/" . substr ( $info ['file'] ['savepath'] . $info ['file'] ['savename'], 8 ) );
			echo $info ['file'] ['savepath'] . $info ['file'] ['savename'];
			$userid = session ( 'user_id' );
			$data ['user_img'] = '/Uploads' . $info ['file'] ['savepath'] . $info ['file'] ['savename'];
			M ( 'user' )->where ( "user_id=$userid" )->field ( 'user_img' )->save ( $data );
		}
	}
	public function ImageCut($path) {
		$imgCut = new \Think\Image ();
		$imgCut->open ( $path );
		$height = $imgCut->height ();
		$width = $imgCut->width ();
		$imgCut->thumb ( 100, 100, \Think\Image::IMAGE_THUMB_CENTER );
		// $imgCut->crop(100, 100,($width - 100) /2,($height - 100) / 2,100,100);
		$imgCut->save ( $path );
	}
	public function updataInfo() { // 这是修改密码
		$this->Plsession ();
		if (IS_POST) {
			$userId = session ( 'user_id' );
			$pass = I ( 'pass', '', 'htmlspecialchars' );
			$password = I ( 'password', '', 'htmlspecialchars' );
			$passdata = I ( 'passdata', '', 'htmlspecialchars' );
			if ($password != $passdata)
				$this->error ( '两次密码不一致' );
			$updata = M ( 'user' )->where ( "user_id=$userId and user_password='$pass'" )->setField ( 'user_password', $password );
			if ($updata) {
				$this->success ( '修改成功' );
			} else {
				$this->error ( '原密码错误' );
			}
		}
	}
	public function updataText() {
		$this->Plsession ();
		if (IS_POST) {
			$text = I ( 'text', '', 'htmlspecialchars' );
			$userId = session ( 'user_id' );
			$updata = M ( 'user' )->where ( "user_id=$userId" )->setField ( 'user_text', $text );
			if ($updata) {
				echo 'true';
			}
		}
	}
	public function fuck() { // 这是标签页面
		$uid = session ( 'user_id' );
		$type = I ( 'type', '1', 'int' );
		if ($type == 1 || $type == 2) {
			$tagId = M ( 'user_tag' )->where ( "user_id=$uid and tag_type=$type" )->getField ( "tag_id", true );
			$this->assign ( 'type', $type );
		} else {
			$this->error ( "非法输入" );
		}
		$tagParent = M ( 'tag' )->select ();
		foreach ( $tagParent as $tP ) {
			$pass = 0;
			foreach ( $tagId as $td ) {
				if ($td == $tP ['tag_id']) {
					$pass = 1;
					break;
				}
			}
			$tagarr [] = array (
					'pass' => $pass,
					'tag_id' => $tP ['tag_id'],
					'tag_parent_id' => $tP ['tag_parent_id'],
					'tag_content' => $tP ['tag_content'] 
			);
		}
		$arr = array ();
		function tagLayer($cate, $pid = 0) {
			foreach ( $cate as $v ) {
				if ($v ['tag_parent_id'] == $pid) {
					$v ['cate'] = tagLayer ( $cate, $v ['tag_id'] );
					$arr [] = $v;
				}
			}
			return $arr;
		}
		$tagList = tagLayer ( $tagarr );
		$this->assign ( 'tag', $tagList );
		$this->display ();
	}
	
	public function updataTag() { // 这是修改标签的方法
		$this->Plsession ();
		if (IS_AJAX) {
			$userId = session ( 'user_id' );
			$type = I ( 'type', '', 'int' );
			if ($type == 1 || $type == 2) {
				
				$tag = I ( 'tag', '', 'htmlspecialchars' );
				$delTag = M ( 'user_tag' )->where ( "user_id=$userId and tag_type=$type" )->delete ();
				foreach ( $tag as $sc ) {
					$dataTag [] = array (
							'user_id' => $userId,
							'tag_type' => $type,
							'tag_id' => $sc 
					);
				}
				M ( 'user_tag' )->field ( 'user_id', 'tag_type', 'tag_id' )->addAll ( $dataTag );
				$TagId = M ( 'user_tag' )->where ( "user_id=$userId and tag_type=$type" )->getField ( "tag_id", true );
				$map ['tag_id'] = array (
						'in',
						$TagId 
				);
				$Tag = M ( 'tag' )->where ( $map )->select ();
				echo json_encode ( $Tag );
			} else {
				$this->error ( '非法操作' );
			}
		}
	}
	
	public function filter(){//这是自定义领域筛选
		$uid = session ( 'user_id' );
		$tagId = M ( 'user_tag' )->where ( "user_id=$uid" )->getField ( "tag_id", true );
		$this->assign ( 'type', $type );
		$tagParent = M ( 'tag' )->select ();
		foreach ( $tagParent as $tP ) {
			$pass = 0;
			foreach ( $tagId as $td ) {
				if ($td == $tP ['tag_id']) {
					$pass = 1;
					break;
				}
			}
			$tagarr [] = array (
					'pass' => $pass,
					'tag_id' => $tP ['tag_id'],
					'tag_parent_id' => $tP ['tag_parent_id'],
					'tag_content' => $tP ['tag_content']
			);
		}
		$arr = array ();
		function tagLayer($cate, $pid = 0) {
			foreach ( $cate as $v ) {
				if ($v ['tag_parent_id'] == $pid) {
					$v ['cate'] = tagLayer ( $cate, $v ['tag_id'] );
					$arr [] = $v;
				}
			}
			return $arr;
		}
		$tagList = tagLayer ( $tagarr );
		$this->assign ( 'tag', $tagList );
		$this->display();
	}
	
	public function myTag($uid) {
		$xqTagId = M ( 'user_tag' )->where ( "user_id=$uid and tag_type=1" )->getField ( "tag_id", true );
		$scTagId = M ( 'user_tag' )->where ( "user_id=$uid and tag_type=2" )->getField ( "tag_id", true );
		$xqmap ['tag_id'] = array (
				'in',
				$xqTagId 
		);
		$scmap ['tag_id'] = array (
				'in',
				$scTagId 
		);
		$xqTag = M ( 'tag' )->where ( $xqmap )->select ();
		$scTag = M ( 'tag' )->where ( $scmap )->select ();
		$this->assign ( 'xqTag', $xqTag );
		$this->assign ( 'scTag', $scTag );
	}
	public function hall() {
		if (! IS_POST) {
			$userId = session ( 'user_id' );//取得当前登录user_id
			// $this->assign('session',md5($userId));
			if (IS_POST) {
				$arrTag = I ( 'tag', '', 'htmlspecialchars' );				
				$mapTag ['tag_id'] = array (
						'in',
						$arrTag
				);
				
				$arrUser = M ( 'user_tag' )->where ( $mapTag )->getField ( 'user_id', true );
				$arrUser = array_flip ( array_flip ( $arrUser ) );
				$mapArticle ['article_user_id'] = array (
						'in',
						$arrUser
				);
				$mapArticle ['article_father_id'] = array (
						'eq',
						'0'
				);
				
				$article = D ( 'article' )->relation ( true )->where ( $mapArticle )->order ( 'article_id desc' )->limit ( 0, 10 )->select ();
				foreach ( $article as $a ) {
					$article_zan = $a ['article_zan'];
					$article_zan_id = $a ['article_zan_id'];
					$article_dao = $a ['article_dao'];
					$article_dao_id = $a ['article_dao_id'];
					$arrOrder = explode ( "@", $a ['article_order'] );
					$order = $a ['article_order'] ? count ( $arrOrder ) : 0;
					
					if (! $userId) {
						$article_zan_id = 0;
						$article_dao_id = 0;
						$is_order = 0;
					} else {
						$arrZan = explode ( "@", $article_zan_id );
						$article_zan_id = in_array ( $userId, $arrZan ) ? 1 : 0;
						$arrDao = explode ( "@", $article_dao_id );
						$article_dao_id = in_array ( $userId, $arrDao ) ? 1 : 0;
						$is_order = in_array ( $userId, $arrOrder ) ? 1 : 0;
					}
					
					$arrCont [] = array (
							'article_id' => $a ['article_id'],
							'article_father_id' => $a ['article_father_id'],
							'article_title' => $a ['article_title'],
							'article_content' => htmlspecialchars_decode ( $a ['article_content'] ),
							'article_type' => $a ['article_type'],
							'article_time' => $a ['article_time'],
							'father_id' => $a ['article_father_id'],
							'article_order' => $order,
							'is_order' => $is_order,
							'article_zan' => $article_zan,
							'article_zan_id' => $article_zan_id,
							'article_dao' => $article_dao,
							'article_dao_id' => $article_dao_id,
							'user_id' => $a ['user_id'],
							'user_email' => $a ['user_email'],
							'user_img' => $a ['user_img'],
							'user_name' => $a ['user_name'],
							'article_num' => $a ['article_num'] 
					);
				}
				$tagName = M ( 'tag' )->where ( $mapTag )->getField ( 'tag_content', true );
				$this->assign ( 'tagName', $tagName );
				$this->assign ( 'list', $arrCont );
			} else {
				
				$mapArticle ['article_father_id'] = array (
						'eq',
						'0' 
				);
				$article = D ( 'article' )->relation ( true )->where ( $mapArticle )->order ( 'article_id desc' )->limit ( 0, 10 )->select ();
				
				foreach ( $article as $a ) {
					$article_zan = $a ['article_zan'];
					$article_zan_id = $a ['article_zan_id'];
					$article_dao = $a ['article_dao'];
					$article_dao_id = $a ['article_dao_id'];
					$arrOrder = explode ( "@", $a ['article_order'] );
					$order = $a ['article_order'] ? count ( $arrOrder ) : 0;
					
					if (! $userId) {
						$article_zan_id = 0;
						$article_dao_id = 0;
						$is_order = 0;
					} else {
						$arrZan = explode ( "@", $article_zan_id );
						$article_zan_id = in_array ( $userId, $arrZan ) ? 1 : 0;
						$arrDao = explode ( "@", $article_dao_id );
						$article_dao_id = in_array ( $userId, $arrDao ) ? 1 : 0;
						$arrOrder = explode ( "@", $a ['article_order'] );
						$is_order = in_array ( $userId, $arrOrder ) ? 1 : 0;
					}
					
					$arrCont [] = array (
							'article_id' => $a ['article_id'],
							'article_father_id' => $a ['article_father_id'],
							'article_title' => $a ['article_title'],
							'article_content' => htmlspecialchars_decode ( $a ['article_content'] ),
							'article_type' => $a ['article_type'],
							'article_time' => $a ['article_time'],
							'father_id' => $a ['article_father_id'],
							'article_order' => $order,
							'is_order' => $is_order,
							'article_zan' => $article_zan,
							'article_zan_id' => $article_zan_id,
							'article_dao' => $article_dao,
							'article_dao_id' => $article_dao_id,
							'user_id' => $a ['user_id'],
							'user_email' => $a ['user_email'],
							'user_img' => $a ['user_img'],
							'user_name' => $a ['user_name'],
							'article_num' => $a ['article_num'] 
					);
				}
				$this->assign ( 'list', $arrCont );
			}
			$tagParent = M ( 'tag' )->select ();
			$arr = array ();
			function tagLayer($cate, $pid = 0) {
				foreach ( $cate as $v ) {
					if ($v ['tag_parent_id'] == $pid) {
						$v ['cate'] = tagLayer ( $cate, $v ['tag_id'] );
						$arr [] = $v;
					}
				}
				return $arr;
			}
			$tagList = tagLayer ( $tagParent );
			$this->assign ( 'tag', $tagList );
			$this->assign('max_id',$arrCont[count($arrCont) - 1]['article_id']);
			$this->title ();
			$this->display ();
		} else

			$this->load_more_article ();
	}
	
	/**
	 * 
	 * @param int $user_id
	 * @param int $type		1:感兴趣的		2:擅长的
	 */
	private function _getTagsFromUserId($user_id,$type){
		$tags = array();
		$map = array();
		switch ($type)
		{
			case 0: 
				$map['think_user_tag.user_id'] = array('eq',$user_id);
			break;
			case 1: 
				$map['think_user_tag.tag_type'] = array('eq',1);
				$map['think_user_tag.user_id'] = array('eq',$user_id);
			break;
			case 2:
				$map['think_user_tag.tag_type'] = array('eq',2);
				$map['think_user_tag.user_id'] = array('eq',$user_id);
			break;
		}

		$tags =  M ( 'tag' )-> join('think_user_tag on think_tag.tag_id = think_user_tag.tag_id')->where($map)->select ();
		return $tags;
	}
	
	private function _getUserIdFromTagId($arrTag,$type){
		switch ($type)
		{
			case 0:
				break;
			case 1:
				$mapTag['tag_type'] = array('eq',1);
				break;
			case 2:
				$mapTag['tag_type'] = array('eq',2);
				break;
		}
		
		$mapTag ['tag_id'] = array (
				'in',
				$arrTag
			);
		$arrUser = M ( 'user_tag' )->where ( $mapTag )->getField ( 'user_id', true );
		$arrUser = array_flip ( array_flip ( $arrUser ) );
		return $arrUser;
	}
	
	public function load_more_article() {
		$userId = session('user_id');
		$count = empty ( $_REQUEST ['count'] ) ? 20 : $_REQUEST ['count'];
		$max_id = empty ( $_REQUEST ["max_id"] ) ? 0 : $_REQUEST ['max_id'];
		
		$tab = I ('tab',null,'');
		$arrUser = array();
		if($tab == '#interest')
		{
			$arrTag = $this->_getTagsFromUserId($userId, 1);
			$tag = array();
			for($i = 0; $i < count($arrTag);$i++)
			{
				$tag[] = $arrTag[$i]['tag_id'];
			}
			$arrUser = $this->_getUserIdFromTagId($tag,2);
			$mapArticle ['article_user_id'] = array (
					'in',
					$arrUser
			);
		}else if($tab == '#goodAt'){
			$arrTag = $this->_getTagsFromUserId($userId, 2);
			$tag = array();
			for($i = 0; $i < count($arrTag);$i++)
			{
				$tag[] = $arrTag[$i]['tag_id'];
			}
			$arrUser = $this->_getUserIdFromTagId($tag,2);
			$mapArticle ['article_user_id'] = array (
					'in',
					$arrUser
			);			
		}else if($tab == '#custom'){
			$tag = I ( 'tags', '', '' );
			$tag = split(',', $tag);
			$arrUser = $this->_getUserIdFromTagId($tag, 2);
			$mapArticle ['article_user_id'] = array (
					'in',
					$arrUser
			);
		}
				
		$mapArticle ['article_father_id'] = array (
				'eq',
				'0' 
		);
		
		$data = $this->get_article_list_map ( $mapArticle, $max_id, $count);
		foreach ( $data as $a ) {
			$article_zan = $a ['article_zan'];
			$article_zan_id = $a ['article_zan_id'];
			$article_dao = $a ['article_dao'];
			$article_dao_id = $a ['article_dao_id'];
			$arrOrder = explode ( "@", $a ['article_order'] );
			$order = $a ['article_order'] ? count ( $arrOrder ) : 0;
						
			if (! $userId) {
						$article_zan_id = 0;
						$article_dao_id = 0;
						$is_order = 0;
			} else {
						$arrZan = explode ( "@", $article_zan_id );
						$article_zan_id = in_array ( $userId, $arrZan ) ? 1 : 0;
						$arrDao = explode ( "@", $article_dao_id );
						$article_dao_id = in_array ( $userId, $arrDao ) ? 1 : 0;
						$arrOrder = explode ( "@", $a ['article_order'] );
						$is_order = in_array ( $userId, $arrOrder ) ? 1 : 0;
					}
			
			$arrCont [] = array (
					'article_id' => $a ['article_id'],
					'article_father_id' => $a ['article_father_id'],
					'article_title' => $a ['article_title'],
					'article_content' => htmlspecialchars_decode ( $a ['article_content'] ),
					'article_type' => $a ['article_type'],
					'article_time' => date ( "Y-m-d H:s", $a ['article_time'] ),
					'father_id' => $a ['article_father_id'],
					'article_order' => $order,
					'is_order' => $is_order,
					'article_zan' => $article_zan,
					'article_zan_id' => $article_zan_id,
					'article_dao' => $article_dao,
					'article_dao_id' => $article_dao_id,
					'user_id' => $a ['user_id'],
					'user_email' => $a ['user_email'],
					'user_img' => $a ['user_img'],
					'user_name' => $a ['user_name'],
					'article_num' => $a ['article_num'] 
			);
		}
		
		$this->ajaxReturn ( $arrCont );
	}
	
	/**
	 * 获得文章列表
	 *
	 * @param number $since_id
	 *        	最大文章id
	 * @param number $page        	
	 * @param number $count        	
	 * @param number $type        	
	 */
	private function get_article_list($page = 1, $count = 10, $type = 0, $since_id = 0) {
		if ($type == 0)
			if ($since_id == 0) {
				// $data = $this->field ($fields)->table ( "{$this->tablePrefix}article AS a LEFT JOIN {$this->tablePrefix}user AS u ON a.user_id= u.user_id" )->order( 'article_id desc')->select ();
				$data = D ( 'article' )->order ( 'article_id desc' )->limit ( ($page - 1) * $count, $count )->relation ( true )->select ();
			} else
				$data = D ( 'article' )->where ( 'article_id < ' . $since_id )->order ( 'article_id desc' )->limit ( ($page - 1) * $count, $count )->relation ( true )->select ();
		else
			$data = D ( 'article' )->where ( 'article_type =' . $type . 'article_id < ' . $since_id )->order ( 'article_id desc' )->limit ( ($page - 1) * $count, $count )->relation ( true )->select ();
		
		return $data;
	}
	private function get_article_list_map($map, $max_id = 0, $count = 10) {
		
		if ($max_id == 0) {
			// $data = $this->field ($fields)->table ( "{$this->tablePrefix}article AS a LEFT JOIN {$this->tablePrefix}user AS u ON a.user_id= u.user_id" )->order( 'article_id desc')->select ();
			$data = D ( 'article' )->relation ( true )->where ( $map )->order ( 'article_id desc' )->limit ( 0, $count )->select ();
		} else {
			$map ['article_id'] = array (
					"lt",
					$max_id 
			);

			$data = D ( 'article' )->relation ( true )->where ( $map )->order ( 'article_id desc' )->limit ( 0 ,$count )->select ();
		}
		
		return $data;
	}
	
	public function search(){
		$words = trim(I ('words','',''));
		if($words != '')
		{
			$articles = $this->_searchArticle($words);
			$users = $this->_searchUser($words);
		}
				
		$this->assign('articles',$articles);
		$this->assign('users',$users);
		$this->assign('words',$words);
		$this->title();
		$this->display();
	}
	
	private function _searchUser($words){
		$words = split(' ', $words);
		$map = '';
		foreach ($words as $i=>$value)
		{
			$map .= " think_user.user_name like '%".$value."%' or think_user.user_text like '%".$value."%' or (c.tag_content like '%".$value."%' and b.tag_type = 2)";
			if($i < count($words) - 1)
				$map .= ' or ';
		}
		$users = M ( 'user' )->join('join think_user_tag as b on think_user.user_id = b.user_id join think_tag as c on b.tag_id = c.tag_id')->where ( $map )->group('think_user.user_id')->order ("FIND_IN_SET(think_user.user_name,'$words[0]'),FIND_IN_SET(think_user.user_text,'$words[0]')" )->select ();
		
		$user_ids = array();
		foreach ($users as $i=>$user){
			$user_ids[] = $user['user_id'];
		}
				
		$map = array();
		$map['think_user.user_id'] = array('in',$user_ids);
		$map['b.tag_type'] = array('eq',2);
		$tags = M('user')->join('join think_user_tag as b on think_user.user_id = b.user_id join think_tag as c on b.tag_id = c.tag_id')->where ( $map )->field ("think_user.user_id,c.tag_content")->select();
		
		$results = $users;
		foreach ($tags as $i=>$tag)
		{
			foreach ($results as $j=>$result)
			{
				if($result['user_id'] == $tag['user_id'])
					$results[$j]['tag'][] = $tag['tag_content'];
			}		
		}
		return $results;
	}
	
	private function _searchArticle($words){
		$userId = session ( 'user_id' );
		$words = split(' ', $words);
		
		$map = '(';
		foreach ($words as $i=>$value)
		{
			$map .= "think_article.article_title like '%".$value."%' or think_article.article_content like '%".$value."%' ";
			if($i < count($words) - 1)
				$map .= ' or ';
		}
		$map .= ') and think_article.article_type in (1,2)';		
		$result1 = D ( 'article' )->relation ( true )->join('think_user as u on think_article.article_user_id = u.user_id')->where ( $map )->order ( "FIND_IN_SET(think_article.article_title,'$words[0]'),FIND_IN_SET(think_article.article_content,'$words[0]'),think_article.article_id desc" )->select();
		
		
		$map = '(';
		foreach ($words as $i=>$value)
		{
			$map .= "a.operate_content	 like '%".$value."%' ";
			if($i < count($words) - 1)
				$map .= ' or ';
		}
		$result2 = D ( 'article' )->relation ( true )->join( 'think_article_operate as a on think_article.article_id = a.article_id' )->where ( $map )->select();
		
		foreach ($result1 as $i=>$a)
		{
			$article[] = array (
					'article_id' => $a ['article_id'],
					'article_father_id' => $a ['article_father_id'],
					'article_user_id' => $a ['article_user_id'],
					'article_title' => $a ['article_title'],
					'article_content' => htmlspecialchars_decode ( $a ['article_content'] ),
					'article_type' => $a ['article_type'],
					'article_time' => $a ['article_time'],
					'article_zan' => $a['article_zan'],
					'article_zan_id' => $a['article_zan_id'],
					'article_dao' => $a['article_dao'],
					'article_dao_id' => $a['article_dao_id'],
					'article_order' => $a['article_order'],
					'user_id' => $a ['user_id'],
					'user_email' => $a ['user_email'],
					'user_img' => $a ['user_img'],
					'user_name' => $a ['user_name'],
					'article_num' => $a ['article_num']
			);
		}

		foreach ($result2 as $i=>$a)
		{
			$article[] = array (
					
					'article_id' => $a ['article_id'],
					'article_father_id' => $a ['article_father_id'],
					'article_user_id' => $a ['article_user_id'],
					'article_title' => $a ['article_title'],
					'article_content' =>  '发表观点：'.htmlspecialchars_decode ( $a ['article_content'] ),
					'article_type' => $a ['article_type'],
					'article_time' => $a ['article_time'],
					'article_zan' => $a['article_zan'],
					'article_zan_id' => $a['article_zan_id'],
					'article_dao' => $a['article_dao'],
					'article_dao_id' => $a['article_dao_id'],
					'article_order' => $a['article_order'],
					'user_id' => $a ['user_id'],
					'user_email' => $a ['user_email'],
					'user_img' => $a ['user_img'],
					'user_name' => $a ['user_name']
			);
		}
	
		foreach ( $article as $a ) {
			$article_zan = $a ['article_zan'];
			$article_zan_id = $a ['article_zan_id'];
			$article_dao = $a ['article_dao'];
			$article_dao_id = $a ['article_dao_id'];
			$arrOrder = explode ( "@", $a ['article_order'] );
			$order = $a ['article_order'] ? count ( $arrOrder ) : 0;
				
			if (! $userId) {
				$article_zan_id = 0;
				$article_dao_id = 0;
				$is_order = 0;
			} else {
				$arrZan = explode ( "@", $article_zan_id );
				$article_zan_id = in_array ( $userId, $arrZan ) ? 1 : 0;
				$arrDao = explode ( "@", $article_dao_id );
				$article_dao_id = in_array ( $userId, $arrDao ) ? 1 : 0;
				$is_order = in_array ( $userId, $arrOrder ) ? 1 : 0;
			}
				
			$arrCont [] = array (
					'article_id' => $a ['article_id'],
					'article_father_id' => $a ['article_father_id'],
					'article_title' => $a ['article_title'],
					'article_content' => htmlspecialchars_decode ( $a ['article_content'] ),
					'article_type' => $a ['article_type'],
					'article_time' => $a ['article_time'],
					'father_id' => $a ['article_father_id'],
					'article_order' => $order,
					'is_order' => $is_order,
					'article_zan' => $article_zan,
					'article_zan_id' => $article_zan_id,
					'article_dao' => $article_dao,
					'article_dao_id' => $article_dao_id,
					'user_id' => $a ['user_id'],
					'user_email' => $a ['user_email'],
					'user_img' => $a ['user_img'],
					'user_name' => $a ['user_name'],
					'article_num' => $a ['article_num']
			);
		}
		
		return $arrCont;
	}
	
//这是POST提交的方法
	public function send_post($url, $post_data) {   
	  
	  //$postdata = http_build_query($post_data);   
	  $options = array(   
		  'http' => array(   
		  'method' => 'POST',   
		  'header' => 'Content-type:application/x-www-form-urlencoded',   
		  'content' => $post_data,   
		  'timeout' => 15 * 60 // 超时时间（单位:s）   
		)   
	  );   
	  $context = stream_context_create($options);   
	  $result = file_get_contents($url, false, $context);   
	  
	  return $result;   
	} 
	public  function send_post_mess(){ 
	 	 //$send_mess_array=M('') 
	  	 $user_id_array=M('session')->join('think_user on think_session.session_id=think_user.session_id')->getField ( 'user_id', true ); 
		 $mess_amp['think_mess.accepe_user_id']=array('in',$user_id_array); 
		 $mess_amp['think_mess.mess_pass']=array('eq','0'); 
		 $send_mess_cont=D('mess')->join('think_article as a on think_mess.article_id = a.article_id')->relation ( true )->where($mess_amp)->field( 
		 		array( 
		 				'think_mess.mess_id', 
		 				'think_mess.article_id', 
		 				'think_mess.mess_type', 
		 				'think_mess.send_user_id', 
		 				'think_mess.accepe_user_id', 
		 				'think_mess.mess_content', 
		 				'think_mess.mess_pass', 
		 				'think_mess.mess_time', 
		 				'a.article_title', 
		 				'a.article_content' 
					) 
	 	)->select(); 
	  
	 for($i = 0;$i < count($send_mess_cont);$i ++) 
	 { 
	 	$send_mess_cont[$i]['article_content'] =  substr ( strip_tags(htmlspecialchars_decode ( $send_mess_cont[$i]['article_content'])), 0, 50 ); 
	 } 
	
	 if($send_mess_cont){
		$data = array();
		foreach($send_mess_cont as $a){
			$data[$a['accepe_user_id']][] = $a;
			
		}
		$post_data = array();
		$key = array_keys($data);
		for($i = 0;$i < count($key);$i++){
			$post_data[$i]['to'] = md5("mess".md5($key[$i]));
			$post_data[$i]['data'] = $data[$key[$i]];
		}
		
	    $post_data=json_encode($post_data);
	    //这个是发送的方法你可以输出来看返回值
	    $this->send_post('http://122.10.87.69:9999', $post_data); 
		
	    }
 }
  //通过静态方法                  
   public  function state_mess(){
	  if(!S('_state')){
	 
	  S('_state','false',3600*24); 
	  }
	  if( S( '_state' ) == 'false' ){
		 S( '_state','true',60 ); 
		 
		   $this->send_post_mess(); 
		   
		   S('_state','false',3600*24);
		   
		  if(S('wait')=='true'){
			  
			   S('wait','false',3600*24); 
			   
			   $this->state_mess();
			  }
	  }else{
		  S('wait','true',60); 
			}
	   }

}	